package com.liuxm.gateway.ratelimit;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author liuxm
 * @version 1.0
 * @date 2021/3/23 23:03
 */
@Component
public class IpKeyResolver implements KeyResolver {
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        ServerHttpRequest request = exchange.getRequest();
        // 远程用户的ip地址
        String ipAddress = request.getRemoteAddress().getHostName();

        // 获取用户
        //Mono<String> map = exchange.getPrincipal().map(Principal::getName);

        // 请求地址
        //String requestUrl = request.getURI().getPath();

        return Mono.just(ipAddress);
    }
}
